package stage_1_module_4.code.homework_5;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.Scanner;

public class ClientOne {
    public static void main(String[] args) {
        Socket s = null;
        int type;
        Message m = null;
        BufferedReader br = null;
        ObjectOutputStream oos = null;
        FileInputStream fis = null;
        DataOutputStream dos = null;
        byte[] bs = null;

        try {


            System.out.println("输入用户昵称:");
            Scanner sc= new Scanner(System.in);
            String sname  = sc.next();

            s = new Socket("127.0.0.1", 8899);
            System.out.println(sname+"连接成功");
            oos = new ObjectOutputStream(s.getOutputStream());
            dos = new DataOutputStream(s.getOutputStream());

            ClientReceiveThread crt = new ClientReceiveThread(s);
            crt.start();
            
            while (true) {
                Thread.sleep(200);
                System.out.println("输入发送类型:1.文本，2.文件");
                type = sc.nextInt();
    
                System.out.println(oos);
                
                if (type == 1) {
                    System.out.println("输入发送内容:");
                    br = new BufferedReader(new InputStreamReader(System.in));
                    String str = br.readLine();
                    if ("bye".equalsIgnoreCase(str)) {
                        System.out.println("聊天结束！");
                        break;
                    }
                    m = new Message(type, str, sname);
                    System.out.println("message:"+m);
                    oos.writeObject(m);
                    // oos.flush();
                } else if (type == 2) {
                    System.out.println("输入发送文件名称:1.txt|2.txt|3.txt");
                    String is = sc.next();
                    if ("bye".equalsIgnoreCase(is)) {
                        System.out.println("聊天结束！");
                        break;
                    }
                    m = new Message(type, is, sname);
                    oos.writeObject(m);
                    oos.flush();
                    String fname = "./stage_1_module_4/code/homework_5/send/"+is;
                    fis = new FileInputStream(fname);
                    bs = fis.readAllBytes();
                    // 输出文件长度
                    dos.writeInt(bs.length);
                    // 传输文件内容
                    dos.write(bs);
                    dos.flush();
                    fis.close();
                }
            }
            crt.stop();
        } catch (IOException | InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            if (null != oos) {
                try {
                    oos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
